package cn.ibizlab.odoo.core.odoo_lunch.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ilunch_product;
import cn.ibizlab.odoo.core.odoo_lunch.domain.Lunch_product;
import cn.ibizlab.odoo.core.odoo_lunch.filter.Lunch_productSearchContext;
import cn.ibizlab.odoo.core.odoo_lunch.service.ILunch_productService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[工作餐产品] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Lunch_productServiceImpl implements ILunch_productService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ilunch_productClientService lunch_productClientService;

    @Override
    public Lunch_product get(Integer id) {
        Ilunch_product clientModel = lunch_productClientService.createModel();
        clientModel.setId(id);
        lunch_productClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Lunch_product get(Lunch_product lunch_product) {
        Lunch_product retlunch_product = this.get(lunch_product.getId()) ;
    	BeanCopier copier=BeanCopier.create(retlunch_product.getClass(), lunch_product.getClass(), false);
        copier.copy(retlunch_product, lunch_product, null);
        return lunch_product ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //lunch_productClientService.removeBatch(lunch_products) ;

    }

    @Override
    public boolean update(Lunch_product lunch_product) {
        boolean bOk = false ;
        Ilunch_product clientModel = this.convert2Model(lunch_product,null);
        lunch_productClientService.update(clientModel) ;
        this.convert2Domain(clientModel,lunch_product) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Lunch_product> lunch_products, int batchSize) {

        //lunch_productClientService.updateBatch(lunch_products,batchSize) ;

    }

    @Override
    public boolean create(Lunch_product lunch_product) {
        boolean bOk = false ;
        Ilunch_product clientModel = this.convert2Model(lunch_product,null);
        lunch_productClientService.create(clientModel) ;
        this.convert2Domain(clientModel,lunch_product);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ilunch_product clientModel = lunch_productClientService.createModel();
        clientModel.setId(id);
        lunch_productClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Lunch_product> lunch_products, int batchSize) {

                        //lunch_productClientService.createBatch(lunch_products,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Lunch_product> searchDefault(Lunch_productSearchContext context) {
        List<Lunch_product> list = new ArrayList<Lunch_product>();
        Page<cn.ibizlab.odoo.core.client.model.Ilunch_product> clientModelList = lunch_productClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ilunch_product clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Lunch_product>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据工作餐产品类别查询
     */
    @Override
    public Page<Lunch_product> selectByOdoo_category(Integer id){
        Lunch_productSearchContext context = new Lunch_productSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Lunch_product> list = new ArrayList<Lunch_product>();
        Page<cn.ibizlab.odoo.core.client.model.Ilunch_product> clientModelList = lunch_productClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ilunch_product clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Lunch_product>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Lunch_product> selectByOdoo_supplier(Integer id){
        Lunch_productSearchContext context = new Lunch_productSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Lunch_product> list = new ArrayList<Lunch_product>();
        Page<cn.ibizlab.odoo.core.client.model.Ilunch_product> clientModelList = lunch_productClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ilunch_product clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Lunch_product>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Lunch_product> selectByOdoo_create(Integer id){
        Lunch_productSearchContext context = new Lunch_productSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Lunch_product> list = new ArrayList<Lunch_product>();
        Page<cn.ibizlab.odoo.core.client.model.Ilunch_product> clientModelList = lunch_productClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ilunch_product clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Lunch_product>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Lunch_product> selectByOdoo_write(Integer id){
        Lunch_productSearchContext context = new Lunch_productSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Lunch_product> list = new ArrayList<Lunch_product>();
        Page<cn.ibizlab.odoo.core.client.model.Ilunch_product> clientModelList = lunch_productClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ilunch_product clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Lunch_product>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ilunch_product convert2Model(Lunch_product domain , Ilunch_product model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = lunch_productClientService.createModel() ;
        }

         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getDescriptionDirtyFlag())
            model.setDescription(domain.getDescription());
         if(domain.getAvailableDirtyFlag())
            model.setAvailable(domain.getAvailable());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getPriceDirtyFlag())
            model.setPrice(domain.getPrice());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getSupplier_textDirtyFlag())
            model.setSupplier_text(domain.getSupplier_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCategory_id_textDirtyFlag())
            model.setCategory_id_text(domain.getCategory_id_text());
         if(domain.getSupplierDirtyFlag())
            model.setSupplier(domain.getSupplier());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCategory_idDirtyFlag())
            model.setCategory_id(domain.getCategory_id());
        return model ;
    }

    public Lunch_product convert2Domain( Ilunch_product model ,Lunch_product domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Lunch_product();
        }

        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getDescriptionDirtyFlag())
            domain.setDescription(model.getDescription());
        if(model.getAvailableDirtyFlag())
            domain.setAvailable(model.getAvailable());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getPriceDirtyFlag())
            domain.setPrice(model.getPrice());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getSupplier_textDirtyFlag())
            domain.setSupplier_text(model.getSupplier_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCategory_id_textDirtyFlag())
            domain.setCategory_id_text(model.getCategory_id_text());
        if(model.getSupplierDirtyFlag())
            domain.setSupplier(model.getSupplier());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCategory_idDirtyFlag())
            domain.setCategory_id(model.getCategory_id());
        return domain ;
    }


}
